package com.orhon.smartcampus.modules.systemctl.fetcher;


import com.orhon.smartcampus.modules.systemctl.dao.MenusDao;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDepartmentUserRelationDao;
import com.orhon.smartcampus.modules.systemctl.entity.*;
import com.orhon.smartcampus.modules.systemctl.gqlModel.*;
import com.orhon.smartcampus.modules.systemctl.mm.*;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DepartmentUsersRecordFetcher implements DataFetcher {


	@Autowired
	OrgDepartmentUserRelationDao orgDepartmentUserRelationDao;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		int id = Integer.parseInt(env.getArgument("id").toString());

		String hql = "from OrgDepartmentUserRelation where deleted_at = null and id = "+id;
		OrgDepartmentUserRelation item =  orgDepartmentUserRelationDao.findId(hql, null);
		if(item!=null) {
			GQLUser sqlUser = null;
			if (env.getSelectionSet().contains("user")){
				Users users = item.getUserUsersByUserId();
				sqlUser = UserMapper.MAPPER.mapUser(users);
			}
			GQLDuty gqlDuty = null;
			if (env.getSelectionSet().contains("duty")){
				 OrgDuties orgDuties = item.getSystemctlOrgDutiesByDutyId();
			 	gqlDuty = DutyMapper.MAPPER.toDuties(orgDuties);
			}

			GQLDepartment sqlDepartment= null;
			if (env.getSelectionSet().contains("department")){
				OrgDepartments orgDepartments = item.getSystemctlOrgDepartmentsByDepartmentId();
				sqlDepartment = DepartmentMapper.MAPPER.toOrgDepartments(orgDepartments);
			}


			GQLDepartmentUsers departmentUsers = DepartmentUsersRecordMapper.MAPPER.toOrgDepartmentUsers(item);
			departmentUsers.setUser(sqlUser);
			departmentUsers.setDuty(gqlDuty);
			departmentUsers.setDepartment(sqlDepartment);

			return departmentUsers;
		}
		return null;


	}



}
